package ie.dkit.semanticPhotoWeb.client.panels;

import java.util.HashMap;

import ie.dkit.semanticPhotoWeb.client.Main;
import ie.dkit.semanticPhotoWeb.client.dtos.AlbumDTO;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.panels.album.AlbumPanel;
import ie.dkit.semanticPhotoWeb.client.panels.photo.PhotoPanel;
import ie.dkit.semanticPhotoWeb.client.tools.LoginListener;
import ie.dkit.semanticPhotoWeb.client.tools.Refreshable;

import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Container;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.TabPanelListenerAdapter;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class CenterPanel extends TabPanel implements LoginListener,Refreshable {

	private HashMap tabs;

	private class CloseListener extends TabPanelListenerAdapter {

		public boolean doBeforeRemove(Container self, Component component) {

			if (component instanceof PhotoPanel) {
				PhotoPanel picturePanel = (PhotoPanel) component;
				picturePanel.deRegisterRelationCreatorListener();
				tabs.remove(picturePanel.getCurrentImageDTO().feedRef);
			} else if (component instanceof AlbumPanel) {
				AlbumPanel albumPanel = (AlbumPanel) component;
				tabs.remove(albumPanel.getCurrentAlbumDTO().feedRef);
			} else if (component instanceof UserOverviewPanel) {
				UserOverviewPanel albumOverviewPanel = (UserOverviewPanel) component;
				tabs.remove(albumOverviewPanel.getCurrentUsername());
			}
			return super.doBeforeRemove(self, component);
		}

	}

	private final Main m_Main;

	private UserOverviewPanel m_AlbumOverviewPanel;

	private CloseListener m_CloseListener;

	private boolean loggedIn;

	public CenterPanel(Main main) {
		m_Main = main;
		tabs = new HashMap();
		m_CloseListener = new CloseListener();
		addListener(m_CloseListener);
		setEnableTabScroll(true);
		add(getUserOverviewPanel());
	}

	/**
	 * Returns the user overview panel of the current logged in user
	 * @return
	 */
	final public UserOverviewPanel getUserOverviewPanel() {
		if (m_AlbumOverviewPanel == null) {
			m_AlbumOverviewPanel = new UserOverviewPanel(m_Main
					.getClientServerConnectorAsync(), this, loggedIn);
		}
		return m_AlbumOverviewPanel;
	}

	/**
	 * Add a albumPanel to the center panel
	 * @param albumDTO
	 */
	public void addAlbumPanel(AlbumDTO albumDTO) {
		if (tabs.containsKey(albumDTO.feedRef)) {
			AlbumPanel albumPanel = (AlbumPanel) tabs.get(albumDTO.feedRef);
			albumPanel.refresh();
			gotoTab(albumPanel);
		} else {
			AlbumPanel albumPanel = new AlbumPanel(albumDTO, m_Main
					.getClientServerConnectorAsync(), this);
			tabs.put(albumDTO.feedRef, albumPanel);
			addTab(albumPanel);
		}
	}

	/**
	 * Add a 
	 * @param imageDTO
	 */
	public void addPhotoPanel(ImageDTO imageDTO) {
		if (tabs.containsKey(imageDTO.feedRef)) {
			PhotoPanel picturePanel = (PhotoPanel) tabs
					.get(imageDTO.feedRef);
			picturePanel.refresh();
			gotoTab(picturePanel);
		} else {
			PhotoPanel photoPanel = new PhotoPanel(imageDTO, m_Main
					.getClientServerConnectorAsync(), this);
			tabs.put(imageDTO.feedRef, photoPanel);
			addTab(photoPanel);
		}
	}

	/**
	 * Add a UserOverviewPanel to the CenterPanel
	 * @param username
	 */
	public void addUserOverviewPanel(String username) {
		if (tabs.containsKey(username)) {
			UserOverviewPanel albumOverviewPanel = (UserOverviewPanel) tabs
					.get(username);
			albumOverviewPanel.refresh();
			gotoTab(albumOverviewPanel);
		} else {
			UserOverviewPanel albumOverviePanel = new UserOverviewPanel(
					m_Main.getClientServerConnectorAsync(), this, username,
					loggedIn);
			tabs.put(username, albumOverviePanel);
			addTab(albumOverviePanel);
		}
	}

	private void addTab(Panel panel) {
		add(panel);
		gotoTab(panel);
	}

	private void gotoTab(Panel panel) {
		activate(panel.getId());
		scrollToTab(panel, true);
	}

	/*
	 * (non-Javadoc)
	 * @see ie.dkit.semanticPhotoWeb.client.tools.Refreshable#refresh()
	 */
	public void refresh() {
		// Refreshes the Users AlbumOverview
		getUserOverviewPanel().refresh();
	}

	/*
	 * (non-Javadoc)
	 * @see ie.dkit.semanticPhotoWeb.client.tools.LoginListener#loggedIn(java.lang.String)
	 */
	public void loggedIn(String username) {
		loggedIn = true;
		getUserOverviewPanel().loadAlbumIcons();
	}

	/*
	 * (non-Javadoc)
	 * @see ie.dkit.semanticPhotoWeb.client.tools.LoginListener#loggedOut()
	 */
	public void loggedOut() {
		loggedIn = false;
		getUserOverviewPanel().showWelcomePage();
	}
}
